<?php

namespace App\Http\Controllers\Admin;

use App\Models\Node;
use Illuminate\Http\Request;

class NodeController extends BaseController
{
    //节点列表显示
    public function index()
    {
        $data = (new Node)->getAllList();
        return view('admin.node.index',compact('data'));
    }

    //节点添加 显示
    public function create()
    {
        $data = Node::where('pid',0)->get();
        return view('admin.node.create',compact('data'));
    }

    //节点添加 处理
    public function store(Request $request)
    {
        try {
            $this->validate($request,[
                'name'  => 'required|unique:nodes,name'
            ]);
        }catch (\Exception $e){
            return ['status' => 1000,'msg'=> '节点名已存在'];
        }
        Node::create($request->except('_token'));
        return ['status' => 0,'msg'=>'添加成功'];





    }

    //
    public function show(Node $node)
    {
        //
    }

    //节点修改 显示
    public function edit(Node $node)
    {
        $data = Node::where('pid',0)->get();
        return  view('admin.node.edit',compact('node','data'));
    }

    //节点修改 处理
    public function update(Request $request, Node $node)
    {
        //
        try {
            $this->validate($request,[
                'name' => 'required|unique:roles,name,' . $node->id . ',id'
            ]);
        }catch (\Exception $E){
            return ['status' =>1000,'msg'=>'节点名已存在'];
        }
        $node->update($request->except('_token'));
        return ['status' => 0,'msg' => '修改节点成功'];
    }

    //加点删除
    public function destroy(Node $node)
    {
        //$node->delete();
        return ['status' => 0,'msg'=>'删除成功'];
    }
}
